
***************************************************************************************************************
/*
THIS DO-FILE (CALLED FROM "GET_DATA_FOR_STRUCTURAL_ESTIMATION.DO"):

 creates statistics of caregiver characteristics
 (for full sample, married, single with kids, single without kids)
 
 many people report multiple caregivers. i prioritize main caregiver as:
 1. if in NH/AL then non-family
 2. otherwise, choose spouse/child/othfam/non-fam based on max hours
 3. otherwise (i.e. if tie in hours) choose spouse>child>othfam>nonfam
*/
***************************************************************************************************************

clear all
set more off
set maxvar 20000

use "HRS_sample.dta", clear
merge 1:m hhidpn using "kidsandhelpers_sample.dta", keep(master match)

*********************************************
*sample restrictions:

keep if inw4==1 		  

keep if age4>=65 & age4!=.	

keep if earn4<=3000
keep if earn5<=3000 | inw5==0
keep if earn6<=3000 | inw6==0
keep if earn7<=3000 | inw7==0
keep if earn8<=3000 | inw8==0
keep if earn9<=3000 | inw9==0
keep if earn10<=3000 | inw10==0
keep if earn11<=3000 | inw11==0
keep if earn12<=3000 | inw12==0

keep if inlist(iwstat5,1,5,6)
keep if inlist(iwstat6,1,5,6)
keep if inlist(iwstat7,1,5,6)
keep if inlist(iwstat8,1,5,6)
keep if inlist(iwstat9,1,5,6)
keep if inlist(iwstat10,1,5,6)
keep if inlist(iwstat11,1,5,6)
keep if inlist(iwstat12,1,5,6)

gen sample_single=( single4==1 & ///
				   (single5==1 | inw5==0) & ///
				   (single6==1 | inw6==0) & ///
				   (single7==1 | inw7==0) & ///
				   (single8==1 | inw8==0) & ///
				   (single9==1 | inw9==0) & ///
   				   (single10==1 | inw10==0) & ///
				   (single11==1 | inw11==0) & ///
				   (single12==1 | inw12==0)  )
*********************************************

*********************************************
*distill helper information
forval w=4/12 {
	
	foreach var in spouse child othfam nonfam {
		bys hhidpn: egen helper_any`var'`w'=max(helper_`var'`w')
		bys hhidpn: egen temphelper_tothpm`var'`w'=sum(helper_hpm`w') if helper_`var'`w'==1
		bys hhidpn: egen helper_tothpm`var'`w'=max(temphelper_tothpm`var'`w')
	}
	
	*pick only one of the helpers based on hours (ties broken below)
	foreach var in child othfam nonfam {
		replace helper_any`var'`w'=0 if helper_anyspouse`w'==1 & helper_tothpmspouse`w'>helper_tothpm`var'`w' & helper_tothpmspouse`w'!=.
	}
	foreach var in spouse othfam nonfam {
		replace helper_any`var'`w'=0 if helper_anychild`w'==1 & helper_tothpmchild`w'>helper_tothpm`var'`w' & helper_tothpmchild`w'!=.
	}	
	foreach var in spouse child nonfam {
		replace helper_any`var'`w'=0 if helper_anyothfam`w'==1 & helper_tothpmothfam`w'>helper_tothpm`var'`w' & helper_tothpmothfam`w'!=.
	}		
	foreach var in spouse child othfam {
		replace helper_any`var'`w'=0 if helper_anynonfam`w'==1 & helper_tothpmnonfam`w'>helper_tothpm`var'`w' & helper_tothpmnonfam`w'!=.
	}
	
	*define paid variable over only non-family members
	gen helper_nonfampaid`w'=helper_paid`w' if helper_nonfam`w'==1
	bys hhidpn: egen helper_anynonfampaid`w'=max(helper_paid`w')
}

keep hhidpn sample_single haskid* helper_any* nh* assliv* adl* anyadl*
duplicates drop //now have one observation per (elderly) person
duplicates list hhidpn
										
reshape long haskid helper_anyspouse helper_anychild helper_anyothfam helper_anynonfam helper_anynonfampaid nh assliv adl anyadl, i(hhidpn) j(wave)	

*overall priority: 1. NH or AL, 2. most hours.
*priority if hours tie: spouse>kid>otherfam>nonfam
foreach var in child othfam nonfam {
	replace helper_any`var'=0 if helper_anyspouse==1
}
foreach var in othfam nonfam {
	replace helper_any`var'=0 if helper_anychild==1
}
replace helper_anynonfam=0 if helper_anyothfam==1

replace helper_anynonfam=1 if (nh==1 | assliv==1)
replace helper_anynonfampaid=1 if (nh==1 | assliv==1)
foreach var in spouse child othfam {
	replace helper_any`var'=0 if (nh==1 | assliv==1)
}	
replace helper_anynonfampaid=. if helper_anynonfam!=1
*********************************************

*TABLE ABOUT CAREGIVER CHARACTERISTICS:	 
sum helper_anyspouse helper_anychild helper_anyothfam helper_anynonfam helper_anynonfampaid 
sum helper_anyspouse helper_anychild helper_anyothfam helper_anynonfam helper_anynonfampaid if sample_single==0
sum helper_anyspouse helper_anychild helper_anyothfam helper_anynonfam helper_anynonfampaid if sample_single==1 & haskid==1	
sum helper_anyspouse helper_anychild helper_anyothfam helper_anynonfam helper_anynonfampaid if sample_single==1 & haskid!=1
